package com.aishua.fire.controller.sysmanager;

import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.aishua.fire.common.ValidateUtil;
import com.aishua.fire.common.anno.ModuleService;
import com.aishua.fire.common.anno.PermMapping;
import com.aishua.fire.common.constant.BigMenuConstants;
import com.aishua.fire.common.constant.ModuleConstants;
import com.aishua.fire.common.exp.ValidateException;
import com.aishua.fire.config.init.Init;
import com.aishua.fire.mapper.dto.AccteamList;
import com.aishua.fire.entity.ateam.AccountteamListSearchInfo;
import com.aishua.fire.entity.ateam.AddAccountteamInfo;
import com.aishua.fire.entity.ateam.UpdAccountteamInfo;
import com.aishua.fire.jpa.entity.Accountteam;
import com.aishua.fire.jpa.entity.Permteam;
import com.aishua.fire.mapper.dto.AteamPermTeamSearchDto;
import com.aishua.fire.service.AccountteamService;
import com.aishua.fire.service.PermteamService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

@Api(value = "系统角色模块",tags = {"系统角色模块接口"})
@RestController
@Slf4j
public class SystemAccounttemController {

	@Autowired
	private AccountteamService accountteamService;
	@Autowired
	private PermteamService permteamService;
	@Autowired
	private Init init;
	@Autowired
	private ValidateUtil validateUtil;
	
	/**
	 * gaoyi 2018-04-11
	 * post http://localhost:8080/accountteam
	 * {
			"accountteamName":"测试账户组",
			"accountteamDesc":"测试账户组描述"
		}
	 * 
	 */
	@PermMapping(onlyLogin = true)
	@ModuleService(totalModule=BigMenuConstants.systemManager,moduleName=ModuleConstants.systemAccountteam)
	@ApiOperation(value="添加账户组之前,获取权限组名称的接口")
	@GetMapping(value = {"/accountteam/before"} )
	public List<Permteam> addBefore() {
		List<Permteam> permteams = permteamService.findIdAndPermTeamName();
		log.info("添加账户组之前日志:{}",permteams);
		return permteams;
    }
	
	@PermMapping(onlyLogin = true)
	@ModuleService(totalModule=BigMenuConstants.systemManager,moduleName=ModuleConstants.systemAccountteam)
	@ApiOperation(value="修改账户组之前,获取权限组名称的接口")
	@GetMapping(value = {"/accountteam/updbefore/{accountteamId}"} )
	public List<Permteam> updBefore(@PathVariable("accountteamId") @ApiParam(name="账户组id",value="类如:0000",required=true) String accountteamId) {
		List<Permteam> permteams = accountteamService.findIdAndNameGuoLv(accountteamId);
		log.info("修改账户组之前日志:{}",permteams);
		return permteams;
    }
	
	/**
	 * gaoyi 2018-04-11
	 * post http://localhost:8080/accountteam
	 * {
			"accountteamName":"测试账户组",
			"accountteamDesc":"测试账户组描述"
		}
	 * @throws ValidateException 
	 * 
	 */
	@ModuleService(totalModule=BigMenuConstants.systemManager,moduleName=ModuleConstants.systemAccountteam)
	@ApiOperation(value="添加_角色信息")
	@PostMapping(value = {"/accountteam"} )
	public void add(@RequestBody @Valid AddAccountteamInfo ateamInfo,BindingResult result) throws ValidateException {
		log.info("添加账户组日志:{}",ateamInfo);
		validateUtil.respErrorMsg(result);
		accountteamService.add(ateamInfo);
//		init.initUrlMap(false);
		
        //init.initDBMap(false);
    }
	
	/**
	 * delete http://localhost:8080/accountteam/1
	 * 
	 */
	@ModuleService(totalModule=BigMenuConstants.systemManager,moduleName=ModuleConstants.systemAccountteam)
	@ApiOperation(value="删除_角色信息")
	@DeleteMapping(value = {"/accountteam/{accountteamId}"} )
	public void delete(@PathVariable("accountteamId") @ApiParam(name="账户组id",value="传入json格式",required=true) String accountteamId) {
		log.info("删除账户组日志:{}",accountteamId);
		accountteamService.delete(accountteamId);
//		init.initUrlMap(false);
       // init.initDBMap(false);
    }
	
	/**
	 * get http://localhost:8080/accountteam/1
	 */
	@ModuleService(totalModule=BigMenuConstants.systemManager,moduleName=ModuleConstants.systemAccountteam)
	@ApiOperation(value="查询_角色详情信息")
	@GetMapping(value = {"/accountteam/{ateamId}"} )
	public Accountteam get(@PathVariable("ateamId") @ApiParam(name="账户组id",value="传入json格式",required=true) String ateamId) {
		log.info("查询账户组详情:{}",ateamId);
		return accountteamService.findRoleId(ateamId);
    }
	
	/**
	 * gaoyi     2018-04-10
	 * post http://localhost:8080/accountteamList
	 * {
			"page":0,
			"ateamName":"测试",
			"size":6
		}
	 */
	@ModuleService(totalModule=BigMenuConstants.systemManager,moduleName=ModuleConstants.systemAccountteam)
	@ApiOperation(value="查询_角色信息列表")
	@PostMapping(value = {"/accountteamList"} )
	public List<AteamPermTeamSearchDto> accountteamList(@RequestBody AccountteamListSearchInfo ateamSearchInfo) {
		log.info("账户组查询列表请求参数:{}",ateamSearchInfo);
		List<AteamPermTeamSearchDto> accountteams = accountteamService.searchList(ateamSearchInfo.getAteamName());
        return accountteams;
    }
	
	/**
	 * gaoyi     2018-04-10
	 * get http://localhost:8080/accountteamList/1
	 */
	@ModuleService(totalModule=BigMenuConstants.systemManager,moduleName=ModuleConstants.systemAccountteam)
	@ApiOperation(value="查询_账户下角色信息列表")
	@GetMapping(value = {"/accountteamList/{accountId}"} )
	public List<Accountteam> getAteamList(@PathVariable("accountId") @ApiParam(name="登录账户id",value="类如:0000",required=true) String accountId) {
		log.info("查询账户id:{}",accountId);
		return accountteamService.getAteamListByAccountId(accountId);
    }
	
/*	*//**
	 * 
	 * get http://localhost:8080/test/1
	 *//*
	@ModuleService(totalModule=BigMenuConstants.systemManager,moduleName=ModuleConstants.systemAccountteam)
	@SupplierBlackUrl
	@ApiOperation(value="测试账户组列表信息接口")
	@GetMapping(value = {"/test/{permId}"} )
	public void test(@PathVariable("permId") String permId) {
		accountteamService.testFindAccountteamByPermId(permId);
		init.initUrlMap(false);
        //init.initDBMap(false);
	}*/
	
	/**
	 * gaoyi     2018-04-10
	 * post http://localhost:8080/accountteamListAll
	 */
	@ModuleService(totalModule=BigMenuConstants.systemManager,moduleName=ModuleConstants.systemAccountteam)
	@ApiOperation(value="查询_全查角色信息列表")
	@GetMapping(value = {"/accountteamListAll"} )
	public List<AteamPermTeamSearchDto> accountteamListAll() {
		log.info("全查账户组");
		List<AteamPermTeamSearchDto> ateamList = accountteamService.searchList(null);
		return ateamList;
    }
	
	
	/**
	 * gaoyi     2018-04-10
	 * get http://localhost:8080/findAccountByaccountteamId/1
	 */
	@ModuleService(totalModule=BigMenuConstants.systemManager,moduleName=ModuleConstants.systemAccountteam)
	@ApiOperation(value="查询_通过角色获取账户信息列表")
	@GetMapping(value = {"/findAccountByaccountteamId/{accountteamId}"} )
	public List<AccteamList> findAccountByaccountteamId(@PathVariable("accountteamId") @ApiParam(name="账户组id",value="类如:0000",required=true) Long accountteamId) {
		log.info("查询账户组id:{}",accountteamId);
		List<AccteamList> accounts = accountteamService.findAccountByaccountteamId(accountteamId);
		return accounts;
    }
	
	/**
	 * gaoyi     2018-04-10
	 * get http://localhost:8080/findAccountByaccountteamId/1
	 */
	@ModuleService(totalModule=BigMenuConstants.systemManager,moduleName=ModuleConstants.systemAccountteam)
	@ApiOperation(value="查询_通过账户查询对应的账户组+账户信息")
	@GetMapping(value = {"/accountteam/findByaccountId/{accountId}"} )
	public Map<String,Object> findByaccountId(@PathVariable("accountId") @ApiParam(name="登录账户id",value="类如:0000",required=true) Long accountId) {
		log.info("查询账户id:{}",accountId);
		Map<String, Object> map = accountteamService.findByAccountIdMap(accountId);
		return map;
    }
	
	  /**
	   * 账户资源组获取下拉列表
	   */
	@PermMapping(onlyLogin = true)
	@ModuleService(totalModule=BigMenuConstants.systemManager,moduleName=ModuleConstants.systemAccountteam)
	@ApiOperation(value="账户资源组获取下拉列表")
	@GetMapping(value = {"/atteamList"})
	public List<Accountteam> getAtteamList() {
		log.info("账户资源组获取下拉列表");
		return accountteamService.getAttList();
	}
	
	@ModuleService(totalModule=BigMenuConstants.systemManager,moduleName=ModuleConstants.systemAccountteam)
	@ApiOperation(value="修改_角色信息")
	@PostMapping(value = {"/accountteam/update"} )
	public void updateAccountteam(@RequestBody @Valid UpdAccountteamInfo updAccountteamInfo,BindingResult result) throws ValidateException {
		log.info("修改用户组参数:{}",updAccountteamInfo);
		validateUtil.respErrorMsg(result);
		accountteamService.updateAteam(updAccountteamInfo);
    }
}
